package com.home.qh.model;

import java.io.Serializable;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONStringer;
import org.hibernate.envers.RevisionEntity;
import org.hibernate.envers.RevisionNumber;
import org.hibernate.envers.RevisionTimestamp;

import com.home.qh.listener.AuditListener;
import com.home.qh.util.DateUtil;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "audit_revision")
@RevisionEntity(AuditListener.class)
public class AuditRevision implements Serializable {
    private static final Log log = LogFactory.getLog(AuditRevision.class);
	private static final long serialVersionUID = 1L;
	private Long id;
    private long timestamp;
	private String username;
	private String revisionTypeTr;
	private String entityNameTr;
	private String logDetailTr;

	@Id
    @GeneratedValue
    @RevisionNumber
    public Long getId() {
        return id;
    }

    @Column(name="perform_timestamp")
    @RevisionTimestamp
    public long getTimestamp() {
        return timestamp;
    }

    @Column(name="username", length=50)
	public String getUsername() { 
		return username;
	}

    public void setId(Long id) {
        this.id = id;
    }
    
	public void setUsername(String username) { 
		this.username = username; 
	}

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    @Transient
    public String getRevisionDateTr() {
        return DateUtil.getDateTime(DateUtil.getDateTimePattern(), new Date(timestamp));
    }
	@Transient
    public String getRevisionTypeTr() {
		return revisionTypeTr;
	}

	public void setRevisionTypeTr(String revisionTypeTr) {
		this.revisionTypeTr = revisionTypeTr;
	}
	
	@Transient
	public String getEntityNameTr() {
		return entityNameTr;
	}

	public void setEntityNameTr(String entityNameTr) {
		this.entityNameTr = entityNameTr;
	}
	@Transient
	public String getLogDetailTr() {
		return logDetailTr;
	}

	public void setLogDetailTr(String logDetailTr) {
		this.logDetailTr = logDetailTr;
	}

    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AuditRevision)) return false;

        AuditRevision that = (AuditRevision) o;

        if (id != that.id) return false;
        if (timestamp != that.timestamp) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (id !=null ? id.intValue() : 0);
        result = 31 * result + (int) (timestamp ^ (timestamp >>> 32));
        return result;
    }

    public String toString() {
        JSONStringer js = new JSONStringer();
        try {
            // Begin array
            js.array();
            js.object().key("id").value(id).endObject();
            js.object().key("username").value(username).endObject();
            js.object().key("timestamp").value(timestamp).endObject();
            js.object().key("entityNameTr").value(entityNameTr).endObject();
            js.object().key("logDetailTr").value(logDetailTr).endObject();
            js.object().key("revisionTypeTr").value(revisionTypeTr).endObject();
            js.endArray();
            // end array
        } catch (JSONException e) {
            if(log.isErrorEnabled())
            	log.error("Error in AuditRevision toString(): \n" + e.getMessage());
        }
        return js.toString();
    }
}
